package com.zut.controller;

import com.zut.pojo.Repay;
import com.zut.pojo.Result;
import com.zut.pojo.User;
import com.zut.service.RepayService;
import com.zut.service.UserService;
import com.zut.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
public class RepayController {

    @Autowired
    private RepayService repayService;
    @Autowired
    private UserService userService;

    @RequestMapping("/addRepay")
    public Result addRepay(@RequestBody Repay repay, HttpServletRequest req){
        System.out.println("repay = " + repay);
        String jwt = req.getHeader("Authorization");
        // 解析令牌
        String phone = (String) JwtUtils.parseJWT(jwt).get("phone");
        User user = userService.telTest(phone);
        repay.setUserId(user.getId());
        Integer integer = repayService.addRepay(repay);
        if(integer!=0){
            return Result.success();
        }else {
            return Result.error("添加回报失败");
        }
    }
}
